String formatTime(int seconds, {String format = ''}) {
  var minutes = seconds ~/ 60;
  var sec = seconds % 60;
  String m = minutes < 10 ? '0' + minutes.toString() : minutes.toString();
  String s = sec < 10 ? '0' + sec.toString() : sec.toString();
  if (format == '') {
    return m + ':' + s;
  }
  return format.replaceFirst('M', m).replaceFirst('S', s);
}

String formatDate(String date) {
  if (date == null || date == '') {
    return '';
  }
  try {
    DateTime d = DateTime.parse(date);
    return d.year.toString() +
        '年' +
        d.month.toString() +
        '月' +
        d.day.toString() +
        '日';
  } catch (e) {
    return '';
  }
}

String formatDate2(String date) {
  if (date == null) {
    return '';
  }
  try {
    var d = DateTime.parse(date).millisecondsSinceEpoch;
    var now = DateTime.now().millisecondsSinceEpoch;
    var seconds = (now - d) ~/ 1000;
    var minute = seconds ~/ 60;
    var hour = minute ~/ 60;
    var day = hour ~/ 24;
    if (day > 0) {
      return '$day天前';
    }
    if (hour > 0) {
      return '$hour小时前';
    }
    if (minute > 0) {
      return '$minute分钟前';
    }
    return '刚刚';
  } catch (e) {
    return '';
  }
}

String formatDuration(Duration position) {
  final ms = position.inMilliseconds;

  int seconds = ms ~/ 1000;
  final int hours = seconds ~/ 3600;
  seconds = seconds % 3600;
  var minutes = seconds ~/ 60;
  seconds = seconds % 60;

  final hoursString = hours >= 10 ? '$hours' : hours == 0 ? '00' : '0$hours';

  final minutesString =
      minutes >= 10 ? '$minutes' : minutes == 0 ? '00' : '0$minutes';

  final secondsString =
      seconds >= 10 ? '$seconds' : seconds == 0 ? '00' : '0$seconds';

  final formattedTime =
      '${hoursString == '00' ? '' : hoursString + ':'}$minutesString:$secondsString';

  return formattedTime;
}
